package com.tianma315.admin.check.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.tianma315.admin.check.dao.RecordArrtDao;
import com.tianma315.admin.check.domain.RecordArrtDO;

import com.tianma315.admin.check.vo.CheckRecordAndArrtVO;
import com.tianma315.admin.check.vo.CheckRecordArrtVO;
import com.tianma315.admin.check.vo.RecordArrtVO;
import com.tianma315.admin.common.base.CoreServiceImpl;
import com.tianma315.admin.common.exception.HtmlCodeException;
import com.tianma315.admin.common.exception.IFastException;
import com.tianma315.admin.common.exception.MessageException;
import com.tianma315.admin.common.type.EnumErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianma315.admin.check.dao.CheckRecordDao;
import com.tianma315.admin.check.domain.CheckRecordDO;
import com.tianma315.admin.check.service.CheckRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 
 * <pre>
 * 检测记录
 * </pre>
 * <small> 2019-05-28 16:30:57 | Aron</small>
 */
@Service
public class CheckRecordServiceImpl extends CoreServiceImpl<CheckRecordDao, CheckRecordDO> implements CheckRecordService {

    @Autowired
    private RecordArrtDao recordArrtDao;

    @Autowired
    private CheckRecordService checkRecordService;

    @Transactional
    @Override
    public Boolean addCheckRecordAndArrt(CheckRecordAndArrtVO recordAndArrtVO) {
        CheckRecordDO checkRecordDO = recordAndArrtVO;
        //每次插入数据前，先把旧数据设置为已删除状态
        Wrapper<CheckRecordDO> recordDOWrapper = new EntityWrapper();
        if (checkRecordDO.getPurchaseId() == null){
            recordDOWrapper.eq("warehousing_record_id",checkRecordDO.getWarehousingRecordId());
        }else{
            recordDOWrapper.eq("purchase_id",checkRecordDO.getPurchaseId());
        }
        checkRecordDO.setState(1);
        update(checkRecordDO,recordDOWrapper);
        //修改之后，把状态修改回来
        checkRecordDO.setState(0);
        insert(checkRecordDO);

        RecordArrtDO recordArrtDO = new RecordArrtDO();
        for (int i = 0; i < recordAndArrtVO.getCheckArrtId().size(); i++) {
            recordArrtDO.setCheckTemplateId(checkRecordDO.getCheckTemplateId());
            recordArrtDO.setArrtValues(recordAndArrtVO.getArrtValues().get(i));
            recordArrtDO.setCheckArrtId(recordAndArrtVO.getCheckArrtId().get(i));
            recordArrtDO.setCheckRecordId(checkRecordDO.getCheckRecordId());
            recordArrtDao.insert(recordArrtDO);
        }

        return null;
    }

    @Override
    public CheckRecordArrtVO getCheckRecordArrtVO(Integer purchaseId) {
        Wrapper<CheckRecordDO> checkRecordDOWrapper = new EntityWrapper<>();
        checkRecordDOWrapper.eq("purchase_id",purchaseId);
        checkRecordDOWrapper.eq("state",0);
        List<CheckRecordDO> list =selectList(checkRecordDOWrapper);
        if (list == null || list.size()<1){
            throw new HtmlCodeException(EnumErrorCode.nullList.getMsg());
        }
        CheckRecordDO checkRecordDO = list.get(0);

        List<RecordArrtVO> recordArrtVOList = recordArrtDao.getRecordArrtVO(checkRecordDO.getCheckRecordId());
        CheckRecordArrtVO checkRecordArrtVO = new CheckRecordArrtVO();
        checkRecordArrtVO.setCheckRecordDO(checkRecordDO);
        checkRecordArrtVO.setRecordArrtVOList(recordArrtVOList);

        return checkRecordArrtVO;
    }

    @Override
    public CheckRecordArrtVO getCheckRecordArrtVOWarehousingRecordId(Integer warehousingRecordId) {
        Wrapper<CheckRecordDO> checkRecordDOWrapper = new EntityWrapper<>();
        checkRecordDOWrapper.eq("warehousing_record_id",warehousingRecordId);
        checkRecordDOWrapper.eq("state",0);
        List<CheckRecordDO> list =selectList(checkRecordDOWrapper);
        if (list == null || list.size()<1){
            throw new HtmlCodeException(EnumErrorCode.nullList.getMsg());
        }
        CheckRecordDO checkRecordDO = list.get(0);

        List<RecordArrtVO> recordArrtVOList = recordArrtDao.getRecordArrtVO(checkRecordDO.getCheckRecordId());
        CheckRecordArrtVO checkRecordArrtVO = new CheckRecordArrtVO();
        checkRecordArrtVO.setCheckRecordDO(checkRecordDO);
        checkRecordArrtVO.setRecordArrtVOList(recordArrtVOList);

        return checkRecordArrtVO;
    }

    @Override
    public CheckRecordArrtVO getCheckRecordArrtVO2(Integer purchaseId) {
        Wrapper<CheckRecordDO> checkRecordDOWrapper = new EntityWrapper<>();
        checkRecordDOWrapper.eq("purchase_id",purchaseId);
        checkRecordDOWrapper.eq("state",0);
        List<CheckRecordDO> list =selectList(checkRecordDOWrapper);
        CheckRecordArrtVO checkRecordArrtVO = new CheckRecordArrtVO();
        if(CollectionUtils.isNotEmpty(list)){
            CheckRecordDO checkRecordDO = list.get(0);
            List<RecordArrtVO> recordArrtVOList = recordArrtDao.getRecordArrtVO(checkRecordDO.getCheckRecordId());
            checkRecordArrtVO.setCheckRecordDO(checkRecordDO);
            checkRecordArrtVO.setRecordArrtVOList(recordArrtVOList);

        }

        return checkRecordArrtVO;
    }

    @Override
    public CheckRecordArrtVO getCheckRecordArrtVOWarehousingRecordId2(Integer warehousingRecordId) {
        Wrapper<CheckRecordDO> checkRecordDOWrapper = new EntityWrapper<>();
        checkRecordDOWrapper.eq("warehousing_record_id",warehousingRecordId);
        checkRecordDOWrapper.eq("state",0);
        List<CheckRecordDO> list =selectList(checkRecordDOWrapper);
        CheckRecordArrtVO checkRecordArrtVO = new CheckRecordArrtVO();
        if(CollectionUtils.isNotEmpty(list)){
            CheckRecordDO checkRecordDO = list.get(0);
            List<RecordArrtVO> recordArrtVOList = recordArrtDao.getRecordArrtVO(checkRecordDO.getCheckRecordId());
            checkRecordArrtVO.setCheckRecordDO(checkRecordDO);
            checkRecordArrtVO.setRecordArrtVOList(recordArrtVOList);
        }


        return checkRecordArrtVO;
    }

    @Transactional
    @Override
    public Boolean updateCheckRecordArrtVO(CheckRecordArrtVO checkRecordArrtVO) {
        CheckRecordDO checkRecordDO = checkRecordArrtVO.getCheckRecordDO();
        Boolean result = updateById(checkRecordDO);
        List<RecordArrtVO> recordArrtVOList = checkRecordArrtVO.getRecordArrtVOList();
        for (int i = 0; i < recordArrtVOList.size(); i++) {
            RecordArrtDO recordArrtDO = recordArrtVOList.get(i);
            if (recordArrtDO.getArrtValues()!=null){
                recordArrtDao.updateRecordArrtDo(recordArrtDO);
            }
        }
        return result;
    }


}
